#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) {
    if (head == NULL)
        return head;
    struct ListNode* phead = malloc(sizeof(struct ListNode));
    phead->next = head;
    struct ListNode* prev = phead, * ptail = phead->next;
    while (ptail != NULL)
    {
        if (ptail->val != val)
        {
            prev->next = ptail;
            prev = prev->next;
        }
        ptail = ptail->next;
    }
    prev->next = ptail;
    return phead->next;
}